package com.shop2cn.iapi.agentproducts.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.SetAgentPriceAutoMarkupBean;
import com.shop2cn.iapi.agentproducts.service.SetAgentPriceAutoMarkupCall;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.SetMarkupConfigBean;
import com.ymatou.iapi.godmch.service.CreateCodeCall;
import com.ymatou.iapi.prodstock.parameter.AgentProductPo;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
/**
*设置/更新商品代理价使用自动加价价格Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SetAgentPriceAutoMarkup{
	private static SetAgentPriceAutoMarkupBean setagentpriceautomarkupBean;
	private static SetAgentPriceAutoMarkupCall setagentpriceautomarkupCall;
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("设置/更新商品代理价使用自动加价价格Test");
	}

	@Before
	public void caseUp() {
		setagentpriceautomarkupBean=new SetAgentPriceAutoMarkupBean();
		setagentpriceautomarkupCall=new SetAgentPriceAutoMarkupCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setagentpriceautomarkupCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetAgentPriceAutoMarkup_001() {
		Logger.start(true,"正确数据，成功");
		try {
			

			/**
			 * 
			 
			// 创建渠道商
			CreateCodeBean createAllMsgSellerMch = GodmchManageCallService.CreateAllMsgSellerMch();
			// 创建渠道商邀请码
			CreateCodeCall createCodeCall2 = GodmchManageCallService.createCodeCall(createAllMsgSellerMch);
			// 创建代理商
			Integer agentUserId = GodmchManageCallService.CreateSellerMch(createCodeCall2.getJsonObject().getAsJsonObject("result").get("code").getAsString());
			int rootUserId = createAllMsgSellerMch.getMchId();
			// 设置自动加价配置
			SetMarkupConfigBean setmarkupconfigBean = initSetMarkupConfigBean(agentUserId, rootUserId);
			setmarkupconfigBean.setDecimalType(3);
			setmarkupconfigBean.setPercentage(new BigDecimal(10));
			setmarkupconfigBean.setMarkupType(2);
			SetMarkupConfig(setmarkupconfigBean);
			// 创建单规格商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId, "autotest", true);
			while (addAgentProductBean.getCatalogList().size() > 1) {
				addAgentProductBean.getCatalogList().remove(0);
			}
			// 设置拿货价 这里的代理所属agenttype默认和第一个关联 所以设置第一个价格
			addAgentProductBean.getCatalogList().get(0).getAgentPriceList().get(0).setAgentPrice(10);
			addAgentProductBean.getCatalogList().get(0).getAgentPriceList().get(1).setAgentPrice(20);
			// 设置直销价
			addAgentProductBean.getCatalogList().get(0).setDirectPrice(155d);
			Logger.debug("addAgentProductBean:" + FormatUtil.beanToGSONString(addAgentProductBean));
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			// 查询总线发送自动导入商品消息
			MessageListBean MessageListBean = MqQueryCallService.initImportAgentproduct(productId, rootUserId);
			List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if (joa.size() == 0) {
				Thread.sleep(5000);
				joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele -> {
				Logger.debug(ele.toString());
			});
			// 查询自动导入的商品的信息
			List<AgentProductPo> ap = ymtProductsIWapper.selectAgentProductByRootProductId(productId).stream().filter(x -> x.getAction() == 0)
					.filter(x -> x.getUserId() == agentUserId).collect(Collectors.toList());
			Logger.debug("ap:" + FormatUtil.beanToGSONString(ap));
			setagentpriceautomarkupBean.setProductId(ap.get(0).getProductId());
			setagentpriceautomarkupBean.setUserId(agentUserId);
			setagentpriceautomarkupCall.setData(setagentpriceautomarkupBean);
			setagentpriceautomarkupCall.callService();
			Logger.verifyEquals("200", setagentpriceautomarkupCall.getString("code"), "验证code");
			BigDecimal p1 = setagentpriceautomarkupCall.getJsonObject().get("data").getAsJsonObject().get("catalogs").getAsJsonArray().get(0).getAsJsonObject().get("price")
					.getAsBigDecimal();
			Logger.verifyEquals(BigDecimal.valueOf(155).setScale(2), p1.setScale(2), "验证价格");

		*/
			
			setagentpriceautomarkupCall.setData(setagentpriceautomarkupBean);
			setagentpriceautomarkupCall.callService();			
			Logger.verifyEquals("200", setagentpriceautomarkupCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}